/**
 * 查询echart数据
 * */
function dataForEchart($this) {
    $.ajax({
        url: '../dataanalysis/showdataAlysis',
        type: 'get',
        data: {
            "dataType": $('#sel-query').val(),
            "recordTime": $(".Wdate:enabled").val(),
            "timeType": $('#date-type a.active').attr('value')
        },
        dataType: "json",
        success: function (res) {
            console.log(res);
            var total = res.data.total;
            if (total == 0) {
                alert('当前时间段没有数据');
                $this.attr('disabled', false).val('查询');
            } else {
                draw_Echart(res);
                $this.attr('disabled', false).val('查询');
            }
        },
        error: function () {
            alert('系统错误请刷新后重试！');
        }
    });
}
/**
 * 创建Echart图表
 * @param {string} dataType 查询类型
 * @param {string} recordTime 查询时间
 * @param {string} timetype 查询时间类型
 * */
function draw_Echart(res) {
    console.log(res);
    var dataType=$('#sel-query').val();
    var recordTime=$(".Wdate:enabled").val();
    var timetype=$('#date-type a.active').attr('value');

    var total = res.data.total;
    var arr_name=res.data.data.horizontalDatas;
    var arr_num=res.data.data.ordinateDatas;

    var title_type='';//标题类别
    var title_date='';//标题日期
    
    /*标题类型*/
    switch (dataType) {
        case "1":
            title_type = "视频播放量";
            break;
        case "2":
            title_type = "资讯访问量";
            break;
        case "3":
            title_type = "栏目访问量";
            break;
        case "4":
            title_type = "客户端访问量";
            break;
    }

    /*标题日期*/
    switch (timetype) {
        case "year":
            title_date = recordTime + '年';
            break;
        case "month":
            title_date = recordTime.substring(0, 4) + '年' + recordTime.substring(5, 7) + '月';
            break;
        case "day":
            title_date = recordTime.substring(0, 4) + '年' + recordTime.substring(5, 7) + '月' + recordTime.substring(8, 10) + '日';
            break;
    }
    //图片盒子宽度控制
    var data_view=$('#data-view');
    console.log(arr_name.length);
    (parseInt(35 * arr_name.length) <= 840) ? data_view.css("width", "840px") : data_view.css("width", 35 * arr_name.length + "px");

    // 基于准备好的dom，初始化echarts实例
    var myChart = echarts.init(document.getElementById('data-view'));

    // 指定图表的配置项和数据
    var option = {
        backgroundColor: '#fff',
        title: {
            x:20,
            y:20,
            text: title_date+title_type,
            subtext: '总计:'+total,
            textStyle: {
                fontFamily: '微软雅黑',
                fontSize: 16,
                color: '#666666'
            }
        },
        legend: {
            data: [{
                name:title_type,
                textStyle: {
                    fontFamily: '微软雅黑',
                    color: '#000'
                }
            }],
            x:600,
            y:20
        },
        xAxis: {
            data: arr_name,
            axisLabel: {
                textStyle: {
                    color: '#666666',
                    fontSize: 12,
                    fontFamily: '微软雅黑'
                },
                rotate: 30,
                interval: 0
            },
            type: 'category'
        },
        tooltip: {
            trigger: 'axis'
        },
        series: [{
            name: title_type,
            data: arr_num,
            type: 'bar',
            barMaxWidth: 35,
            markPoint: {
                data: [
                    {type: 'max', name: '最大值'},
                    {type: 'min', name: '最小值'}
                ]
            },
            markLine: {
                data: [
                    {type: 'average', name: '平均值'}
                ]
            }
        }],
        toolbox: {
            show : true,
            feature : {
                mark : {show: true},
                dataView : {show: true, readOnly: false},
                magicType : {show: true, type: ['line', 'bar']},
                restore : {show: true},
                saveAsImage : {show: true}
            }
        },
        calculable : true,
        yAxis: [{
            axisLabel: {
                textStyle: {
                    color: '#666666',
                    fontSize: 12,
                    fontFamily: '微软雅黑'
                },
                interval: 0
            },
            type: 'value',
            splitArea: {show: true}
        }]
    };

    // 使用刚指定的配置项和数据显示图表。
    myChart.setOption(option);
}